home *** CD-ROM | disk | FTP | other *** search
- #include "Binhex.h"
- #include <Errors.h>
-
-
- #define EARLYEOF -1
- #define BADHEXCHAR -2
- #define BADEOF -3
- #define BADPARAM -4
- #define NOZERO -5
- #define USERCANCEL -6
-
- static short vRefNum;
-
-
- static short GetSFTypeCreatorFlags(FSSpec *myFSS,long *theType,long *theCreator,unsigned short *theFlags)
- {
- short errCode;
- FInfo fndrInfo;
-
- *theType = 0L;
- *theCreator = 0L;
- *theFlags = 0;
-
- errCode = FSpGetFInfo(myFSS,&fndrInfo);
- if (errCode == noErr)
- {
- *theType = fndrInfo.fdType;
- *theCreator = fndrInfo.fdCreator;
- *theFlags = fndrInfo.fdFlags;
- }
-
- return(errCode);
- }
-
- static short SetSFTypeCreatorFlags(FSSpec *myFSS,long theType,long theCreator,unsigned short theFlags)
- {
- short errCode;
- FInfo fndrInfo;
-
- errCode = FSpGetFInfo(myFSS,&fndrInfo);
-
- if (errCode == noErr)
- {
- fndrInfo.fdType = theType;
- fndrInfo.fdCreator = theCreator;
- fndrInfo.fdFlags = theFlags;
-
- errCode = FSpSetFInfo(myFSS,&fndrInfo);
- }
- return(errCode);
- }
-
- static short GetDataForkLength(FSSpec *myFSS,long *dataForkLength)
- {
- short errCode,fRefNum;
- long byteCount;
-
- errCode = noErr;
- *dataForkLength = 0L;
-
- errCode = FSpOpenDF(myFSS,0,&fRefNum);
- errCode = GetEOF(fRefNum,&byteCount);
- errCode = FSClose(fRefNum);
-
- *dataForkLength = byteCount;
-
- return(errCode);
- }
-
- static short GetResourceForkLength(FSSpec *myFSS,long *resForkLength)
- {
- short errCode,fRefNum;
- long byteCount;
-
- errCode = noErr;
- *resForkLength = 0L;
-
- errCode = FSpOpenRF(myFSS,0,&fRefNum);
- errCode = GetEOF(fRefNum,&byteCount);
- errCode = FSClose(fRefNum);
-
- *resForkLength = byteCount;
-
- return(errCode);
- }
-
-
- static short OpenAndZeroDataFork(long theType,long theCreator,FSSpec *myFSS,short *theFRefNum)
- {
- short errCode;
-
- *theFRefNum = 0;
-
- errCode = FSpOpenDF(myFSS,0,theFRefNum);
- if (errCode == fnfErr)
- {
- errCode = FSpCreate(myFSS,theCreator,theType,-1);
- errCode = FSpOpenDF(myFSS,0,theFRefNum);
- }
- errCode = SetEOF(*theFRefNum,0L);
- errCode = SetFPos(*theFRefNum,fsFromStart,0L);
- /* Om fel, FSClose(*theFRefNum);*theFRefNum = 0;*/
-
- return(errCode);
- }
-
- static short OpenAndZeroResourceFork(long theType,long theCreator,FSSpec *myFSS,short *theFRefNum)
- {
- short errCode;
-
- *theFRefNum = 0;
-
- errCode = FSpOpenRF(myFSS,0,theFRefNum);
-
- if (errCode == fnfErr)
- {
- errCode = FSpCreate(myFSS,theCreator,theType,-1);
- errCode = FSpOpenRF(myFSS,0,theFRefNum);
- }
- errCode = SetEOF(*theFRefNum,0L);
- errCode = SetFPos(*theFRefNum,fsFromStart,0L);
-
- return(errCode);
- }
-
-
- //---
-
- #define CR ((unsigned char) 0x0D)
- #define LF ((unsigned char) 0x0A)
- #define TAB ((unsigned char) 0x09)
- #define SPACE ((unsigned char) 0x20)
- #define RLFLAG ((unsigned char) 0x90)
-
- #define HEXTYPE 'TEXT'
- #define HEXCREATOR '????'
-
- #define BUFFSIZE 512
-
- enum { PHASE62, PHASE44, PHASE26 };
- enum { PHASE06, PHASE24, PHASE42, PHASE60 };
-
-
-
- static char *gStartOfBinhex = "(This file must be converted with BinHex";
- static char *gBinhexHeader = "(This file must be converted with BinHex 4.0)";
- static char *gEndOfPart = "--- end of part";
- static char gBinHexCodes[256],*gBinHexChars = "!\"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr";
- static unsigned char *gNextHexCharPtr;
- static unsigned char gCurrentReadByte,gCurrentWriteByte;
- static unsigned char gPrevChar,gTheByte;
- static short gChar90Count;
- static short gHexPhase,gLeftIndex,gRightIndex;
- static unsigned short gCRC;
- static char gWriteBuffer[BUFFSIZE],gReadBuffer[BUFFSIZE];
- static long gHexBytesLeftToRead,gReadBufferBytesLeft;
- static long gWriteBufferIndex,gBuffer64Count;
- static short gWriteFRefNum,gReadFRefNum;
- //static char gSaveToDisk,gReadFromDisk;
- static Handle gBinhexHandle;
- static long gBinhexHandleSize,gBinhexIndex;
- static Ptr gHexReadPtr;
-
- static Callback gStartup, gProgress;
- static long gTotal;
-
-
- static short GetNextReadByte(void);
- static short ShiftReadBuffer(void);
- static void CalcCRC(unsigned char theByte);
- static short WriteBinhexBuffer(void);
- static short PutByteToBinhexBuffer(void);
- static short FlushBinhexBuffer(void);
- static short SkipBreak(void);
- static short SkipJunkHexData(void);
- static short ExtractByteFromHexData(void);
- static short GetNextLogicalHexDataByte(char doCRC);
- static short FindStartOfHexData(void);
- static short FindEndOfHexData(void);
- static void SetupBinhexDataArray(void);
-
-
- static short GetNextReadByte(void)
- {
- register short errCode;
-
- errCode = noErr;
- if (gHexBytesLeftToRead <= 0)
- {
- errCode = EARLYEOF;
- return(errCode);
- }
-
- if (gReadBufferBytesLeft <= 0)
- {
- if (gHexBytesLeftToRead > BUFFSIZE)
- gReadBufferBytesLeft = BUFFSIZE;
- else
- gReadBufferBytesLeft = gHexBytesLeftToRead;
-
- /* Good place to update a progress bar? */
- if (gProgress != NULL)
- {
- (*gProgress)(gTotal); // ATT FIXA: argument som anger hur långt vi kommit!
- gTotal = gTotal + BUFFSIZE;
- }
-
- errCode = FSRead(gReadFRefNum,&gReadBufferBytesLeft,(Ptr) gReadBuffer);
- if (errCode != noErr)
- return(errCode);
-
- gNextHexCharPtr = (unsigned char *) gReadBuffer;
- }
-
- gCurrentReadByte = *gNextHexCharPtr++;
- gReadBufferBytesLeft--;
- gHexBytesLeftToRead--;
-
- return(errCode);
- }
-
-
- static short ShiftReadBuffer(void)
- {
- register short errCode;
- long byteCount;
-
- errCode = noErr;
-
- if (gHexBytesLeftToRead <= 0)
- {
- errCode = EARLYEOF;
- return(errCode);
- }
-
- if (gReadBufferBytesLeft > 0)
- BlockMove((Ptr) gNextHexCharPtr,(Ptr) gReadBuffer,gReadBufferBytesLeft);
-
- if (gHexBytesLeftToRead > BUFFSIZE - gReadBufferBytesLeft)
- byteCount = BUFFSIZE - gReadBufferBytesLeft;
- else
- byteCount = gHexBytesLeftToRead;
-
-
- errCode = FSRead(gReadFRefNum,&byteCount,(Ptr) gReadBuffer + gReadBufferBytesLeft);
-
- if (errCode != noErr)
- return(errCode);
-
- gNextHexCharPtr = (unsigned char *) gReadBuffer;
- gReadBufferBytesLeft += byteCount;
-
- return(errCode);
- }
-
-
- // Replaced assembly with simple C code for PPC portability
- static void CalcCRC(unsigned char v)
- {
- int i;
- Boolean temp;
-
- for (i=1; i<=8; i++)
- {
- temp=((gCRC)&0x8000)!=0;
- gCRC=((gCRC)<<1)|((v>>7)&0x0001);
- if (temp)
- (gCRC)=(gCRC)^0x1021;
- v=(v<<1)&0x00ff;
- }
- }
-
- // A few ANSI replacements
-
- static char * strcpy(char *dest, const char *src)
- {
- for (;*src != 0;*dest++ = *src++);
- *dest = 0;
- }
-
- static long strlen(const char *s)
- {
- long len = 0;
-
- for(;s[len] != 0; len++);
- return len;
- }
-
- static char * strcat(char *dest, const char *src)
- {
- for (;*dest != 0; dest++);
- for (;*src != 0;*dest++ = *src++);
- *dest = 0;
- }
-
- static char * strncpy(char * dst, const char * src, long n)
- {
- const char * p = src;
- char * q = dst;
-
- n++;
-
- while (--n)
- if (!(*q++ = *p++))
- {
- while (--n)
- *q++ = 0;
- break;
- }
-
- return(dst);
- }
-
- static void myCtoPstr(char *s)
- {
- char *p;
- long count;
-
- count = 0;
- p = s;
- while (*p)
- {
- p++;
- count++;
- }
- BlockMove(s, &s[1], count);
- s[0] = count;
- }
-
-
- /***************/
-
-
- static short WriteBinhexBuffer(void)
- {
- register short errCode;
-
- errCode = noErr;
-
- errCode = FSWrite(gWriteFRefNum,&gWriteBufferIndex,(Ptr) gWriteBuffer);
- gWriteBufferIndex = 0;
-
- return(errCode);
- }
-
-
- static short PutByteToBinhexBuffer(void)
- {
- register short errCode;
- register unsigned char origByte;
-
- errCode = noErr;
- origByte = gCurrentWriteByte;
-
- CalcCRC(gCurrentWriteByte);
-
- do
- {
- do
- {
- if (gWriteBufferIndex >= BUFFSIZE - 2)
- {
- errCode = WriteBinhexBuffer();
-
- if (errCode != noErr)
- return(errCode);
- }
-
-
- if (gHexPhase == PHASE06)
- {
- gLeftIndex = (gCurrentWriteByte >> 2) & 0x003F;
- gRightIndex = gCurrentWriteByte;
-
- gWriteBuffer[gWriteBufferIndex++] = gBinHexChars[gLeftIndex];
-
- gHexPhase = PHASE24;
- }
-
- else if (gHexPhase == PHASE24)
- {
- gLeftIndex = ((gRightIndex << 4) & 0x0030) | ((gCurrentWriteByte >> 4) & 0x000F);
- gRightIndex = gCurrentWriteByte;
-
- gWriteBuffer[gWriteBufferIndex++] = gBinHexChars[gLeftIndex];
-
- gHexPhase = PHASE42;
- }
-
- else if (gHexPhase == PHASE42)
- {
- gLeftIndex = ((gRightIndex << 2) & 0x003C) | ((gCurrentWriteByte >> 6) & 0x0003);
- gRightIndex = gCurrentWriteByte;
-
- gWriteBuffer[gWriteBufferIndex++] = gBinHexChars[gLeftIndex];
-
- gHexPhase = PHASE60;
- }
-
- else if (gHexPhase == PHASE60)
- {
- gLeftIndex = gRightIndex & 0x003F;
-
- gWriteBuffer[gWriteBufferIndex++] = gBinHexChars[gLeftIndex];
-
- gHexPhase = PHASE06;
- }
-
- if (++gBuffer64Count >= 64)
- {
- gWriteBuffer[gWriteBufferIndex++] = '\r';
- gBuffer64Count = 0;
- }
-
- } while (gHexPhase == PHASE06);
-
- if (origByte == RLFLAG)
- {
- if (gCurrentWriteByte == RLFLAG)
- gCurrentWriteByte = 0;
- else
- origByte = 0;
- }
-
- } while (origByte == RLFLAG);
-
- return(errCode);
- }
-
-
-
- static short FlushBinhexBuffer(void)
- {
- short errCode;
-
- errCode = 0;
-
- if (gWriteBufferIndex >= BUFFSIZE - 2)
- errCode = WriteBinhexBuffer();
-
- gTheByte = 0;
-
- if (gHexPhase == PHASE24)
- {
- gLeftIndex = ((gRightIndex << 4) & 0x0030) | ((gTheByte >> 4) & 0x000F);
- gRightIndex = gTheByte;
-
- gWriteBuffer[gWriteBufferIndex++] = gBinHexChars[gLeftIndex];
- }
-
- else if (gHexPhase == PHASE42)
- {
- gLeftIndex = ((gRightIndex << 2) & 0x003C) | ((gTheByte >> 6) & 0x0003);
- gRightIndex = gTheByte;
-
- gWriteBuffer[gWriteBufferIndex++] = gBinHexChars[gLeftIndex];
- }
-
- else if (gHexPhase == PHASE60)
- {
- gLeftIndex = gRightIndex & 0x003F;
- gWriteBuffer[gWriteBufferIndex++] = gBinHexChars[gLeftIndex];
- }
-
- errCode = WriteBinhexBuffer();
-
- return(errCode);
- }
-
-
-
- static short SkipBreak(void)
- {
- register short errCode,dashCount;
- register unsigned char *s1,*s2;
-
- errCode = noErr;
-
- if (gCurrentReadByte == '-')
- {
- if (gReadBufferBytesLeft < strlen(gEndOfPart))
- {
- errCode = ShiftReadBuffer();
-
- if (errCode != noErr)
- return(errCode);
- }
-
- s1 = (unsigned char *) gEndOfPart + 1L;
- s2 = gNextHexCharPtr;
-
- while (*s1 == *s2 && *s1)
- {
- s1++;
- s2++;
- }
-
- if (*s1 == 0)
- {
- gNextHexCharPtr += strlen(gEndOfPart);
- gReadBufferBytesLeft -= strlen(gEndOfPart);
- gHexBytesLeftToRead -= strlen(gEndOfPart);
-
- if ((errCode = GetNextReadByte()) != noErr)
- return(errCode);
-
- dashCount = 0;
-
- while (dashCount < 3)
- {
- if (gCurrentReadByte == '-')
- dashCount++;
- else
- dashCount = 0;
-
- if ((errCode = GetNextReadByte()) != noErr)
- return(errCode);
- }
-
- dashCount = 0;
-
- while (dashCount < 3)
- {
- if (gCurrentReadByte == '-')
- dashCount++;
- else
- dashCount = 0;
-
- if ((errCode = GetNextReadByte()) != noErr)
- return(errCode);
- }
-
- errCode = SkipJunkHexData();
- }
- }
-
- return(errCode);
- }
-
-
-
-
- static short SkipJunkHexData(void)
- {
- register short errCode;
- register unsigned char ch;
-
- errCode = noErr;
-
- ch = gCurrentReadByte;
-
- if (ch == CR || ch == LF || ch == SPACE || ch == TAB)
- {
- do
- {
- if ((errCode = GetNextReadByte()) != noErr)
- return(errCode);
-
- ch = gCurrentReadByte;
-
- } while (ch == CR || ch == LF || ch == SPACE || ch == TAB);
-
- if (gCurrentReadByte == '-')
- errCode = SkipBreak();
- }
-
- return(errCode);
- }
-
-
-
- static short ExtractByteFromHexData(void)
- {
- register short errCode;
-
- gLeftIndex = gRightIndex;
-
- if ((errCode = GetNextReadByte()) != noErr)
- return(errCode);
-
- if ((errCode = SkipJunkHexData()) != noErr)
- return(errCode);
-
- if (gCurrentReadByte == ':')
- {
- errCode = EARLYEOF;
- return(errCode);
- }
-
- gRightIndex = gBinHexCodes[gCurrentReadByte];
-
- if (gRightIndex >= 64)
- {
- errCode = BADHEXCHAR;
- return(errCode);
- }
-
- if (gHexPhase == PHASE62)
- {
- if ((errCode = GetNextReadByte()) != noErr)
- return(errCode);
-
- if ((errCode = SkipJunkHexData()) != noErr)
- return(errCode);
-
- gLeftIndex = gRightIndex;
-
- if (gCurrentReadByte == ':')
- {
- errCode = EARLYEOF;
- return(errCode);
- }
-
- gRightIndex = gBinHexCodes[gCurrentReadByte];
-
- if (gRightIndex >= 64)
- {
- errCode = BADHEXCHAR;
- return(errCode);
- }
-
- gTheByte = ((gLeftIndex << 2) & 0xFC) | ((gRightIndex >> 4) & 0x03);
- gHexPhase = PHASE44;
- }
-
- else if (gHexPhase == PHASE44)
- {
- gTheByte = ((gLeftIndex << 4) & 0xF0) | ((gRightIndex >> 2) & 0x0F);
- gHexPhase = PHASE26;
- }
-
- else if (gHexPhase == PHASE26)
- {
- gTheByte = ((gLeftIndex << 6) & 0xC0) | (gRightIndex & 0x3F);
- gHexPhase = PHASE62;
- }
-
- return(errCode);
- }
-
-
-
-
- static short GetNextLogicalHexDataByte(char doCRC)
- {
- register short errCode;
-
- errCode = noErr;
-
- if (gChar90Count > 0)
- {
- gTheByte = gPrevChar;
- gChar90Count--;
-
- if (doCRC)
- CalcCRC(gTheByte);
- else
- CalcCRC(0);
- }
-
- else
- {
- do
- {
- errCode = ExtractByteFromHexData();
-
- if (gTheByte != RLFLAG)
- break;
-
- else
- {
- errCode = ExtractByteFromHexData();
-
- if (errCode != noErr)
- return(errCode);
-
- if (gTheByte == 0)
- {
- gTheByte = RLFLAG;
- break;
- }
-
- else if (gTheByte >= 2)
- {
- gChar90Count = gTheByte - 2;
- gTheByte = gPrevChar;
- break;
- }
- }
-
- } while (true);
-
-
- gPrevChar = gTheByte;
-
- if (doCRC)
- CalcCRC(gTheByte);
- else
- CalcCRC(0);
- }
-
- return(errCode);
- }
-
-
- static short FindStartOfHexData(void)
- {
- register short errCode,i;
- char foundStart;
-
- errCode = noErr;
-
- foundStart = false;
-
- if ((errCode = GetNextReadByte()) != noErr)
- return(errCode);
-
- while (!foundStart)
- {
- while (gCurrentReadByte != '(')
- {
- if ((errCode = GetNextReadByte()) != noErr)
- return(errCode);
- }
-
- i = 0;
-
- while (gStartOfBinhex[i] && gCurrentReadByte == gStartOfBinhex[i])
- {
- if ((errCode = GetNextReadByte()) != noErr)
- return(errCode);
- i++;
- }
-
- if (gStartOfBinhex[i] == 0)
- {
- while (gCurrentReadByte != ':')
- {
- if ((errCode = GetNextReadByte()) != noErr)
- return(errCode);
- }
-
- foundStart = true;
- }
- }
-
- gHexPhase = PHASE62;
- gPrevChar = 0;
-
- return(errCode);
- }
-
-
- static short FindEndOfHexData(void)
- {
- register short errCode;
-
- errCode = noErr;
-
- SkipJunkHexData();
-
- if ((errCode = GetNextReadByte()) != noErr)
- return(errCode);
-
- while (gCurrentReadByte == '!')
- {
- SkipJunkHexData();
-
- if ((errCode = GetNextReadByte()) != noErr)
- return(errCode);
- }
-
- if (gCurrentReadByte != ':')
- {
- errCode = BADEOF;
- }
-
- return(errCode);
- }
-
-
- static void SetupBinhexDataArray(void)
- {
- register short i;
-
- for (i=0;i<256;i++)
- gBinHexCodes[i] = 64;
-
- for (i=0;i<64;i++)
- gBinHexCodes[gBinHexChars[i]] = i;
- }
-
-
- pascal short ConvertFromBinhex(FSSpec *inFSS, Boolean deleteBinHex, Boolean deleteOld, Callback startup, Callback progress)
- // Boolean readFromDisk,Ptr theBinhexPtr,long dataLength)
- {
- register unsigned long i;
- short errCode, ignoreErr;
- unsigned short crc;
- unsigned short theFlags;
- unsigned long dataForkLength,resForkLength;
- Str255 fileName;
- unsigned char nameLength;
- long theType,theCreator;
- FSSpec outFSS;
- //StandardFileReply theReply;
- FInfo fndrInfo;
-
- gReadFRefNum = 0;
- gWriteFRefNum = 0;
- gReadBufferBytesLeft = 0L;
- gHexBytesLeftToRead = 0L;
- gChar90Count = 0;
- gPrevChar = 0;
-
- gStartup = startup;
- gProgress = progress;
-
- SetupBinhexDataArray();
-
- errCode = GetDataForkLength(inFSS,&gHexBytesLeftToRead);
- if (errCode != noErr)
- goto BINHEXERR;
-
- /* Good place to update a progress bar? */
- if (gStartup != NULL)
- (*gStartup)(gHexBytesLeftToRead); // ATT FIXA: argument som anger hur långt vi kommit!
- gTotal = 0;
-
- errCode = FSpOpenDF(inFSS,0,&gReadFRefNum);
- if (errCode != noErr)
- goto BINHEXERR;
-
- errCode = FindStartOfHexData();
- if (errCode != noErr)
- goto BINHEXERR;
-
- gCRC = 0;
-
- errCode = GetNextLogicalHexDataByte(true);
-
- if (errCode != noErr)
- goto BINHEXERR;
-
- nameLength = gTheByte;
-
- for (i=0;i<nameLength && errCode == noErr;i++)
- {
- errCode = GetNextLogicalHexDataByte(true);
-
- if (errCode != noErr)
- goto BINHEXERR;
-
- fileName[i] = gTheByte;
- }
-
- errCode = GetNextLogicalHexDataByte(true);
-
- if (errCode != noErr)
- goto BINHEXERR;
-
- if (gTheByte != 0)
- {
- errCode = NOZERO;
- goto BINHEXERR;
- }
-
- fileName[i] = gTheByte;
-
- theType = 0L;
-
- for (i=0;i<4 && errCode == noErr;i++)
- {
- errCode = GetNextLogicalHexDataByte(true);
-
- if (errCode != noErr)
- goto BINHEXERR;
-
- theType = (theType << 8) | gTheByte;
- }
-
- theCreator = 0L;
-
- for (i=0;i<4 && errCode == noErr;i++)
- {
- errCode = GetNextLogicalHexDataByte(true);
-
- if (errCode != noErr)
- goto BINHEXERR;
-
- theCreator = (theCreator << 8) | gTheByte;
- }
-
- errCode = GetNextLogicalHexDataByte(true);
-
- if (errCode != noErr)
- goto BINHEXERR;
-
- theFlags = gTheByte;
-
- errCode = GetNextLogicalHexDataByte(true);
- if (errCode != noErr)
- goto BINHEXERR;
-
- theFlags = (theFlags << 8) | gTheByte;
- dataForkLength = 0L;
-
- for (i=0;i<4 && errCode == noErr;i++)
- {
- errCode = GetNextLogicalHexDataByte(true);
-
- if (errCode != noErr)
- goto BINHEXERR;
-
- dataForkLength = (dataForkLength << 8) | gTheByte;
- }
-
- resForkLength = 0L;
-
- for (i=0;i<4 && errCode == noErr;i++)
- {
- errCode = GetNextLogicalHexDataByte(true);
- if (errCode != noErr)
- goto BINHEXERR;
-
- resForkLength = (resForkLength << 8) | gTheByte;
- }
-
- errCode = GetNextLogicalHexDataByte(false);
- if (errCode != noErr)
- goto BINHEXERR;
-
- crc = gTheByte;
- errCode = GetNextLogicalHexDataByte(false);
-
- if (errCode != noErr)
- goto BINHEXERR;
-
- crc = (crc << 8) | gTheByte;
- if (crc != gCRC)
- goto BINHEXERR;
-
- myCtoPstr(fileName);
- // StandardPutFile("\pSave File As:",fileName,&theReply);
- outFSS = *inFSS;
- BlockMove(fileName, outFSS.name, fileName[0]+1);
-
- // Check if the file exists
- errCode = FSpGetFInfo(&outFSS,&fndrInfo);
-
- // If it does exist, delete!
- // Mod. PhC 06/09/00
- if (errCode == noErr)
- {
- if (deleteOld) {
- errCode = FSpDelete(&outFSS);
- } else {
- errCode = -1; // dummy error code
- }
- // StandardPutFile("\pSave File As:",fileName,&theReply);
-
- // if (!theReply.sfGood)
- if (noErr != errCode)
- goto NOBINHEXERR;
- // outFSS = theReply.sfFile;
- }
-
- errCode = OpenAndZeroDataFork(theType,theCreator,&outFSS,&gWriteFRefNum);
-
- if (errCode != noErr)
- goto BINHEXERR;
-
- gWriteBufferIndex = 0;
- gCRC = 0;
-
- for (i=0;i<dataForkLength;i++)
- {
- errCode = GetNextLogicalHexDataByte(true);
- if (errCode != noErr)
- goto BINHEXERR;
-
- if (gWriteBufferIndex >= BUFFSIZE)
- {
- if (gWriteBufferIndex > 0)
- errCode = FSWrite(gWriteFRefNum,&gWriteBufferIndex,(Ptr) gWriteBuffer);
-
- if (errCode != noErr)
- goto BINHEXERR;
-
- gWriteBufferIndex = 0;
- }
-
- gWriteBuffer[gWriteBufferIndex++] = gTheByte;
- }
-
- if (gWriteBufferIndex > 0)
- {
- errCode = FSWrite(gWriteFRefNum,&gWriteBufferIndex,(Ptr) gWriteBuffer);
- if (errCode != noErr)
- goto BINHEXERR;
-
- gWriteBufferIndex = 0;
- }
-
- errCode = FSClose(gWriteFRefNum);
- gWriteFRefNum = 0;
- if (errCode != noErr)
- goto BINHEXERR;
-
- errCode = GetNextLogicalHexDataByte(false);
- if (errCode != noErr)
- goto BINHEXERR;
-
- crc = gTheByte;
-
- errCode = GetNextLogicalHexDataByte(false);
- if (errCode != noErr)
- goto BINHEXERR;
-
- crc = (crc << 8) | gTheByte;
-
- if (crc != gCRC)
- {
- goto BINHEXERR;
- }
-
- errCode = OpenAndZeroResourceFork(theType,theCreator,&outFSS,&gWriteFRefNum);
-
- if (errCode != noErr)
- goto BINHEXERR;
-
- gWriteBufferIndex = 0;
- gCRC = 0;
-
- for (i=0;i<resForkLength;i++)
- {
- errCode = GetNextLogicalHexDataByte(true);
- if (errCode != noErr)
- goto BINHEXERR;
-
- if (gWriteBufferIndex >= BUFFSIZE)
- {
- if (gWriteBufferIndex > 0)
- errCode = FSWrite(gWriteFRefNum,&gWriteBufferIndex,(Ptr) gWriteBuffer);
-
- if (errCode != noErr)
- {
- goto BINHEXERR;
- }
- gWriteBufferIndex = 0;
- }
- gWriteBuffer[gWriteBufferIndex++] = gTheByte;
- }
-
-
- if (gWriteBufferIndex > 0)
- {
- errCode = FSWrite(gWriteFRefNum,&gWriteBufferIndex,(Ptr) gWriteBuffer);
-
- if (errCode != noErr)
- {
- goto BINHEXERR;
- }
- gWriteBufferIndex = 0;
- }
-
- errCode = FSClose(gWriteFRefNum);
- gWriteFRefNum = 0;
-
- if (errCode != noErr)
- {
- goto BINHEXERR;
- }
-
- errCode = GetNextLogicalHexDataByte(false);
- if (errCode != noErr)
- goto BINHEXERR;
-
- crc = gTheByte;
-
- errCode = GetNextLogicalHexDataByte(false);
- if (errCode != noErr)
- goto BINHEXERR;
-
- crc = (crc << 8) | gTheByte;
-
- if (crc != gCRC)
- {
- goto BINHEXERR;
- }
-
- errCode = FindEndOfHexData();
- if (errCode != noErr)
- goto BINHEXERR;
-
- errCode = SetSFTypeCreatorFlags(&outFSS,theType,theCreator,theFlags);
- if (errCode != noErr)
- goto BINHEXERR;
-
- if (errCode == noErr)
- goto NOBINHEXERR;
-
-
- BINHEXERR:
-
- NOBINHEXERR:
-
- if (gReadFRefNum)
- {
- if (errCode == noErr)
- {
- errCode = FSClose(gReadFRefNum);
- }
- else
- ignoreErr = FSClose(gReadFRefNum);
-
- gReadFRefNum = 0;
- }
-
- if (gWriteFRefNum)
- {
- if (errCode == noErr)
- {
- errCode = FSClose(gWriteFRefNum);
- }
- else
- ignoreErr = FSClose(gWriteFRefNum);
-
- gWriteFRefNum = 0;
- }
-
- if (errCode == USERCANCEL)
- errCode = noErr;
-
- // Mod. PhC 06/09/00
- // delete the hqx file if required
- if (noErr == errCode) {
- if (deleteBinHex) {
- ignoreErr = FSpDelete(inFSS);
- }
- }
-
- // Mod. PhC 06/09/00
- // Save the newly decoded file's FSSpec in the inFSS parameter
- BlockMoveData(&outFSS, inFSS, sizeof(FSSpec));
-
-
- return(errCode);
- }
-
-
- pascal short ConvertToBinhex(FSSpec *inFSS, Callback startup, Callback progress)
- //pascal short ConvertToBinhex(Boolean saveToDisk,Handle *theBinhexHandle)
- {
- long i;
- long dataForkLength,resForkLength;
- short errCode, ignoreErr;
- FSSpec outFSS;
- unsigned short crc;
- unsigned short theFlags;
- Str255 fileName;
- unsigned char nameLength;
- long theType,theCreator;
- FInfo fndrInfo;
- //StandardFileReply theReply;
-
- gReadFRefNum = 0;
- gWriteFRefNum = 0;
-
- gStartup = startup;
- gProgress = progress;
-
- errCode = GetSFTypeCreatorFlags(inFSS,&theType,&theCreator,&theFlags);
-
- if (errCode != noErr)
- goto BINHEXERR;
-
- inFSS->name[inFSS->name[0]+1] = 0;
- strcpy(fileName, inFSS->name+1);
-
- if (strlen(fileName) >= 27)
- fileName[27] = 0;
-
- // strncat(fileName,".hqx",32);
- strcat(fileName,".hqx");
- myCtoPstr(fileName);
- outFSS = *inFSS;
- BlockMove(fileName, outFSS.name, fileName[0]+1);
-
- // Check if the file exists
- errCode = FSpGetFInfo(&outFSS,&fndrInfo);
-
- // If it does exist, delete!
- // If it does exist, delete!
- // Mod. PhC 06/09/00
- if (errCode == noErr)
- {
- errCode = FSpDelete(&outFSS);
- // StandardPutFile("\pSave File As:",fileName,&theReply);
-
- // if (!theReply.sfGood)
- if (noErr != errCode)
- goto NOBINHEXERR;
- // outFSS = theReply.sfFile;
- }
-
- errCode = OpenAndZeroDataFork(HEXTYPE,HEXCREATOR,&outFSS,&gWriteFRefNum);
-
- if (errCode != noErr)
- goto BINHEXERR;
-
- gWriteBufferIndex = 0;
-
- strncpy(gWriteBuffer,gBinhexHeader,BUFFSIZE);
- gWriteBufferIndex = strlen(gWriteBuffer);
-
- gWriteBuffer[gWriteBufferIndex++] = '\r';
- gWriteBuffer[gWriteBufferIndex++] = ':';
-
- errCode = WriteBinhexBuffer();
-
- if (errCode != noErr)
- goto BINHEXERR;
-
- gWriteBufferIndex = 0;
- gCRC = 0;
- gBuffer64Count = 1;
- gHexPhase = PHASE06;
-
- inFSS->name[inFSS->name[0]+1] = 0;
- strcpy(fileName, inFSS->name+1);
- nameLength = strlen(fileName);
-
- gCurrentWriteByte = (char) nameLength;
-
- if ((errCode = PutByteToBinhexBuffer()) != noErr)
- goto BINHEXERR;
-
-
- for (i=0;i<=nameLength;i++)
- {
- gCurrentWriteByte = fileName[i];
-
- if ((errCode = PutByteToBinhexBuffer()) != noErr)
- goto BINHEXERR;
- }
-
-
- errCode = GetSFTypeCreatorFlags(inFSS,&theType,&theCreator,&theFlags);
-
- if (errCode != noErr)
- goto BINHEXERR;
-
-
- for (i=0;i<4;i++)
- {
- gCurrentWriteByte = (theType >> 24) & 0xFF;
-
- if ((errCode = PutByteToBinhexBuffer()) != noErr)
- goto BINHEXERR;
-
- theType <<= 8;
- }
-
-
- for (i=0;i<4;i++)
- {
- gCurrentWriteByte = (theCreator >> 24) & 0xFF;
-
- if ((errCode = PutByteToBinhexBuffer()) != noErr)
- goto BINHEXERR;
-
- theCreator <<= 8;
- }
-
- for (i=0;i<2;i++)
- {
- gCurrentWriteByte = (theFlags >> 8) & 0xFF;
-
- if ((errCode = PutByteToBinhexBuffer()) != noErr)
- goto BINHEXERR;
-
- theFlags <<= 8;
- }
-
-
- errCode = GetDataForkLength(inFSS,&dataForkLength);
- if (errCode != noErr)
- goto BINHEXERR;
-
-
- for (i=0;i<4;i++)
- {
- gCurrentWriteByte = (dataForkLength >> 24) & 0xFF;
-
- if ((errCode = PutByteToBinhexBuffer()) != noErr)
- goto BINHEXERR;
-
- dataForkLength <<= 8;
- }
-
- errCode = GetResourceForkLength(inFSS,&resForkLength);
- if (errCode != noErr)
- goto BINHEXERR;
-
-
- /* Good place to update a progress bar? */
- if (gStartup != NULL)
- (*gStartup)(dataForkLength + resForkLength);
- gTotal = 0;
-
-
- for (i=0;i<4;i++)
- {
- gCurrentWriteByte = (resForkLength >> 24) & 0xFF;
-
- if ((errCode = PutByteToBinhexBuffer()) != noErr)
- goto BINHEXERR;
-
- resForkLength <<= 8;
- }
-
- CalcCRC(0);
- CalcCRC(0);
-
- crc = gCRC;
-
- for (i=0;i<2;i++)
- {
- gCurrentWriteByte = (crc >> 8) & 0xFF;
-
- if ((errCode = PutByteToBinhexBuffer()) != noErr)
- goto BINHEXERR;
-
- crc <<= 8;
- }
-
-
- errCode = GetDataForkLength(inFSS,&dataForkLength);
-
- if (errCode != noErr)
- goto BINHEXERR;
-
- errCode = FSpOpenDF(inFSS,0,&gReadFRefNum);
-
- if (errCode != noErr)
- goto BINHEXERR;
-
- gCRC = 0;
- gReadBufferBytesLeft = 0L;
- gHexBytesLeftToRead = dataForkLength;
-
- for (i=0;i<dataForkLength;i++)
- {
- if ((errCode = GetNextReadByte()) != noErr)
- goto BINHEXERR;
-
- gCurrentWriteByte = gCurrentReadByte;
-
- if ((errCode = PutByteToBinhexBuffer()) != noErr)
- goto BINHEXERR;
- }
-
- if ((errCode = FSClose(gReadFRefNum)) != noErr)
- {
- gReadFRefNum = 0;
- goto BINHEXERR;
- }
-
- gReadFRefNum = 0;
-
-
- CalcCRC(0);
- CalcCRC(0);
-
- crc = gCRC;
-
- for (i=0;i<2;i++)
- {
- gCurrentWriteByte = (crc >> 8) & 0xFF;
-
- if ((errCode = PutByteToBinhexBuffer()) != noErr)
- goto BINHEXERR;
-
- crc <<= 8;
- }
-
-
- errCode = GetResourceForkLength(inFSS,&resForkLength);
-
- if (errCode != noErr)
- goto BINHEXERR;
-
- errCode = FSpOpenRF(inFSS,0,&gReadFRefNum);
-
- if (errCode != noErr)
- goto BINHEXERR;
-
- gCRC = 0;
- gReadBufferBytesLeft = 0L;
- gHexBytesLeftToRead = resForkLength;
-
- for (i=0;i<resForkLength;i++)
- {
- if ((errCode = GetNextReadByte()) != noErr)
- goto BINHEXERR;
-
- gCurrentWriteByte = gCurrentReadByte;
-
- if ((errCode = PutByteToBinhexBuffer()) != noErr)
- goto BINHEXERR;
- }
-
-
- if ((errCode = FSClose(gReadFRefNum)) != noErr)
- {
- gReadFRefNum = 0;
- goto BINHEXERR;
- }
-
- gReadFRefNum = 0;
-
-
- CalcCRC(0);
- CalcCRC(0);
-
- crc = gCRC;
-
- for (i=0;i<2;i++)
- {
- gCurrentWriteByte = (crc >> 8) & 0xFF;
-
- if ((errCode = PutByteToBinhexBuffer()) != noErr)
- goto BINHEXERR;
-
- crc <<= 8;
- }
-
-
- errCode = FlushBinhexBuffer();
-
- if (errCode != noErr)
- goto BINHEXERR;
-
-
- gWriteBufferIndex = 0;
-
- gWriteBuffer[gWriteBufferIndex++] = ':';
- gWriteBuffer[gWriteBufferIndex++] = '\r';
-
-
- errCode = WriteBinhexBuffer();
-
- if (errCode != noErr)
- goto BINHEXERR;
-
- if (errCode == noErr)
- goto NOBINHEXERR;
-
-
-
-
- BINHEXERR:
- if (gBinhexHandle != NULL)
- {
- DisposeHandle(gBinhexHandle);
- }
-
- gBinhexHandle = 0L;
-
-
- NOBINHEXERR:
- if (gWriteFRefNum != 0)
- {
- if (errCode == noErr)
- {
- errCode = FSClose(gWriteFRefNum);
- }
- else
- ignoreErr = FSClose(gWriteFRefNum);
-
- gWriteFRefNum = 0;
- }
-
- if (gReadFRefNum != 0)
- {
- if (errCode == noErr)
- {
- errCode = FSClose(gReadFRefNum);
- }
-
- else
- ignoreErr = FSClose(gReadFRefNum);
-
- gReadFRefNum = 0;
- }
-
- if (errCode == USERCANCEL)
- errCode = noErr;
-
- return(errCode);
- }
-